<template>
  <el-dialog
    v-model="visible"
    title="文章详情"
    width="800px"
    :close-on-click-modal="false"
    destroy-on-close
  >
    <div v-if="article" class="article-detail">
      <h2 class="article-title">{{ article.title }}</h2>

      <div class="author-info">
        <div class="author-top-row">
          <img class="avatar" :src="article.authorAvatar" />
          <div class="author-text">
            <div class="name">{{ article.authorName }}</div>
            <div class="publish-time">{{ formatTime(article.createTime) }}</div>
          </div>
        </div>
        <div class="topic-tags">
          <span class="topic" v-for="(name, id) in article.topicMap" :key="id"
            >#{{ name }}</span
          >
        </div>
      </div>

      <div class="article-content">
        <div
          v-for="item in article.contentList"
          :key="item.orderIndex"
          class="content-block"
        >
          <p v-if="item.contentType === 1" class="text">
            {{ item.textContent }}
          </p>
          <img
            v-else-if="item.contentType === 2"
            :src="item.imageUrl"
            class="image"
          />
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import { computed, toRefs } from 'vue'
import dayjs from 'dayjs'

// props
const props = defineProps({
  modelValue: Boolean,
  article: Object,
})

// emits
const emit = defineEmits(['update:modelValue'])

// 双向绑定 visible
const visible = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val),
})

const formatTime = (time) => dayjs(time).format('YYYY-MM-DD HH:mm')
</script>

<style scoped>
.article-title {
  font-size: 22px;
  font-weight: bold;
  margin-bottom: 16px;
  text-align: center;
}

.author-info {
  display: flex;
  flex-direction: column;
  margin-bottom: 20px;
}

.author-top-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  object-fit: cover;
}

.name {
  font-size: 14px;
  font-weight: bold;
}

.publish-time {
  font-size: 12px;
  color: #999;
  margin-top: 4px;
}

.topic-tags {
  display: flex;
  flex-wrap: wrap;
  margin-top: 8px;
  gap: 6px;
}

.topic {
  font-size: 12px;
  background-color: #f2f2f2;
  padding: 3px 8px;
  border-radius: 12px;
}

.article-content {
  margin-top: 16px;
}

.content-block {
  margin-bottom: 18px;
}

.text {
  font-size: 15px;
  line-height: 1.8;
  color: #333;
}

.image {
  width: 100%;
  border-radius: 6px;
  object-fit: cover;
}
</style>
